﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ADECMS
{
    public partial class _Default : System.Web.UI.Page
    {
        ADECMSDBDataContext cmsdb;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                cmsdb = new ADECMSDBDataContext();
                NEW novost = cmsdb.NEWs.OrderByDescending(x => x.id).First();
                ClanakNaslov.Text = novost.title;
                ClanakText.Text = novost.text;
                ClanakAutorDat.Text = novost.USER.username + ", " + novost.news_date;
            }
            catch (Exception ex)
            {
                ClanakNaslov.Text = " Trenutno nema članaka ";
            }
        }

        protected void LogIn_Click(object sender, EventArgs e)
        {
            try
            {

                USER usr = cmsdb.USERs.Single(u => u.username == KorisnickoIme.Text);
                if (usr.ban == true)
                {
                    Label1.Text = "Zabranjen vam je pristup na site, kontaktirate administratora";
                    return;
                }
                if (usr.password == Sifra.Text)
                {
                    FormsAuthentication.Initialize();
                    FormsAuthentication.SetAuthCookie(KorisnickoIme.Text, false);
                    Response.Redirect("/UserPages/Start.aspx");
                }
                else
                    Label1.Text = "Pogrešna šifra";
            }
            catch (InvalidOperationException)
            {
                Label1.Text = "Pogrešno korisničko ime";
            }
        }

        protected void Registracija_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Registration.aspx");
        }

        protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "BtnClick")
            {
                ClanakNaslov.Text = e.CommandArgument.ToString();
                //Response.Write(e.CommandArgument);
                NEW nov = cmsdb.NEWs.Single(n => n.title == ClanakNaslov.Text);
                ClanakText.Text = nov.text;
                ClanakAutorDat.Text = nov.USER.name + ", " + nov.news_date;

            }
        }
    }
}